package com.weixing.mall.base.utils;

import com.weixing.mall.base.enums.IBaseEnum;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * 枚举工具类
 *
 * @author  siashan
 * @since  v1.0
 **/
public class EnumUtil {

    @Deprecated
    public static <T extends IBaseEnum> T getIEnum(Class<T> targerType, String source) {
        for (T enumObj : targerType.getEnumConstants()) {
            if (source.equals(String.valueOf(enumObj.getValue()))) {
                return enumObj;
            }
        }
        return null;
    }

    /**
     * 方法描述: 根据value得到对应的枚举值
     *
     * @param targerType    目标类
     * @param source        值
     * @return  T 枚举值
    */
    public static <T extends IBaseEnum> T getIEnum(Class<T> targerType, Serializable source) {
        for (T enumObj : targerType.getEnumConstants()) {
            if (source.equals(enumObj.getValue())) {
                return enumObj;
            }
        }
        return null;
    }

    /**
     * 方法描述: 根据value得到对应的描述值
     *
     * @param targerType    目标类
     * @param source        值
     * @return 枚举面熟
    */
    public static String getDesp(Class targerType, Serializable source){
        IBaseEnum iEnum = getIEnum(targerType, source);
        Assert.notNull(iEnum,"value不存在");
        return iEnum.getDesp();
    }





}
